/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.searchWindow
{
	import com.edi.runtime.component.EDIBasicData;
	
	import mx.collections.ListCollectionView;
	import mx.controls.Alert;
	
	public class EDIConditionsRow extends Object
	{
		public var _fields:XMLList;
		public var field:String="";
		public var logic:String="等于";
		public var fieldType:String="";
		public var type:String="";
		public var dataProvider:Object=null;
		public var labelField:String="";
		public var dataField:String="";
		
		public var saveField:String="";
		public var treeIDField:String="";
		public var treepidField:String="";
		public var displayField:String="";
		public var treeLabelField:String="";
		public var remoteService:String="";
		public var treeItemID:String="";
		public var treeRootValue:String="";
		public var itemParentID:String="";
		public var itemID:String="";
		public var dataBaseBtn:EDIBasicData;
		
		public var conditionsIndex:int=0;
		public var conditions:String="请选择"
		public var handlerVisible:Boolean=false;
		public var logicIndex:int=0;
		
		private var _value:String="";
		private var _fieldValue:String="";
		private var _logicValue:String="=";
		private var _conditionsValue:String="";
		
		public var tableName:String="";
		 
		public  var handlerID:String="";
		
		
		private var window:EDISearchWindow;

		public function EDIConditionsRow(window:EDISearchWindow)
		{
			this.window=window;
		}
		
		public function set fields(value:XMLList):void
		{
			_fields=value;
			if(_fields.length()>0)
			{
				fieldValue=_fields[0]["@value"].toString();
				field=_fields[0]["@key"].toString();
				fieldType=_fields[0]["@fieldType"].toString();
				type=_fields[0]["@type"].toString();
				if(XML(_fields[0]).children().length()>0)
				{
					dataProvider=XML(_fields[0]).children();
					labelField=_fields[0]["@labelField"].toString();
					dataField=_fields[0]["@dataField"].toString();
				}
				if(_fields[0].hasOwnProperty("@saveField"))
				{
					saveField=_fields[0]["@saveField"].toString();
					treeIDField=_fields[0]["@treeIDField"].toString();
					treepidField=_fields[0]["@treepidField"].toString();
					displayField=_fields[0]["@displayField"].toString();
					treeLabelField=_fields[0]["@treeLabelField"].toString();
					remoteService=_fields[0]["@remoteService"].toString();
					treeItemID=_fields[0]["@treeItemID"].toString();
					treeRootValue=_fields[0]["@treeRootValue"].toString();
					itemParentID=_fields[0]["@itemParentID"].toString();
					itemID=_fields[0]["@itemID"].toString();
				}
				if(_fields[0].hasOwnProperty("@tableName"))
				{
					labelField=_fields[0]["@labelField"].toString();
					dataField=_fields[0]["@dataField"].toString();
					tableName=_fields[0]["@tableName"].toString();
				}
			}
		}
		
		
		
		public function get fields():XMLList
		{
			return _fields;
		}
		
		public function set btnHandlerVisible(value:String):void
		{
			handlerVisible=value=="handler";
			if(handlerVisible)
			{
				_value="";
			}
		}
		
		public function get logics():XMLList{
			return new XML(
				<db>
					<row  key="等于">
						<value>
							<![CDATA[=]]>	
						</value>
					</row>
					<row  key="大于">
						<value>
							<![CDATA[>]]>	
						</value>
					</row>
					<row  key="小于">
						<value>
							<![CDATA[<]]>	
						</value>
					</row>
					<row  key="包含">
						<value>
							<![CDATA[like]]>	
						</value>
					</row>
				</db>
			).children();
		}
		
		public function get condits():XMLList{
			return XML(
				<db>
					<row key="请选择" value="" />
					<row key="并且" value="and" />
					<row key="或者" value="or" />
				</db>
			).children();
		}
		
		public function set value(value:String):void
		{
			if(_value!=value)
			{
				_value=value;
			}
		}
		public function get value():String
		{
			return _value;
		}
		
		public function set fieldValue(value:String):void
		{
			if(_fieldValue!=value)
			{
				_fieldValue=value;
			}
		}
		public function get fieldValue():String
		{
			return _fieldValue;
		}
		
		public function set logicValue(value:String):void
		{
			if(_logicValue!=value)
			{
				_logicValue=value;
				if(value=="="){logicIndex=0;}
				else if(value==">"){logicIndex=1}
				else if(value=="<"){logicIndex=2}
				else {logicIndex=3}
			}
		}
		public function get logicValue():String
		{
			return _logicValue;
		}
		
		public function set conditionsValue(value:String):void
		{
			if(_conditionsValue!=value)
			{
				if(value=="")
				{
					if(window.conditions.selectedItem!=null)
					{
						if(window.conditions.selectedIndex+1!=(window.conditions.dataProvider as ListCollectionView).length)
						{
							mx.controls.Alert.show("该逻辑表达式无法修改！","系统提示");
							return ;
						}
					}
				}
				
				conditions=value=="and"?"并且":"或者";
				conditionsIndex=value==""?0:(value=="and"?1:2);
				_conditionsValue=value;
			}
		}
		
		public function get conditionsValue():String
		{
			return _conditionsValue;
		}
		
		public function isSuffice():Boolean
		{
			return fieldValue!="" && logicValue!="" && _conditionsValue !="" && value!= ""
		}
		
		public function isNoConditionsSuffice():Boolean
		{
			return fieldValue!="" && logicValue!="" && value!= ""
		}
	}
}